package com.tour.controller.index;

import com.tour.controller.*;

import com.tour.dao.*;

import com.tour.model.*;

import com.tour.service.*;

import com.tour.util.*;
import com.tour.util.PageUtils;

import org.springframework.beans.factory.annotation.Autowired;

import org.springframework.stereotype.Controller;

import org.springframework.ui.ModelMap;

import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;


@Controller
@RequestMapping("/commonapi/index")
public class IndexGlInfoListController {
    @Autowired
    TypeInfoMapper typeInfoMapper;
    @Autowired
    GlInfoMapper glInfoMapper;
    @Autowired
    UserInfoMapper userInfoMapper;

    /**
            进入搜索列表接口
    **/
    @RequestMapping(value = "")
    public String index(ModelMap modelMap, Integer type,
        HttpServletRequest request) {
        LoginModel login = (LoginModel) request.getSession()
                                               .getAttribute(CommonVal.sessionName);
        modelMap.addAttribute("login", login);
        modelMap.addAttribute("type", type);

        TypeInfoExample te = new TypeInfoExample();
        List<TypeInfo> tl = typeInfoMapper.selectByExample(te);
        modelMap.addAttribute("tl", tl);

        return "index/gl_info_list";
    }

    @RequestMapping(value = "search")
    @ResponseBody
    public Object search(String searchWord, Integer type,
        Integer page, HttpServletRequest request) {
        LoginModel login = (LoginModel) request.getSession()
                                               .getAttribute(CommonVal.sessionName);
        int pageSize = 8;

        if (page == null) {
            page = 1;
        }

        GlInfoExample te = new GlInfoExample();
        GlInfoExample.Criteria tc = te.createCriteria();

        if (type != null) {
            tc.andGlTypeEqualTo(type);
        }

        tc.andCheckStatusEqualTo(2);//审核通过的攻略
        if(login==null){
        	tc.andGlLevelEqualTo(1);//全部人可见
        }else if(login.getLoginType()==2){
        	UserInfo user = userInfoMapper.selectByPrimaryKey(login.getId());
        	if(user.getUserLevel()==1){
        		tc.andGlLevelEqualTo(1);
        	}
        }
        if ((searchWord != null) && (searchWord.trim().equals("") == false)) {
            tc.andTitleLike("%" + searchWord + "%");
        }

        int count = (int) glInfoMapper.countByExample(te);
        int totalPage = 0;

        if ((count > 0) && ((count % pageSize) == 0)) {
            totalPage = count / pageSize;
        } else {
            totalPage = (count / pageSize) + 1;
        }

        te.setPageRows(pageSize);
        te.setStartRow((page - 1) * pageSize);

        List<GlInfo> tl = glInfoMapper.selectByExample(te);
        List<Map<String, Object>> list = new ArrayList<Map<String, Object>>();

        for (GlInfo t : tl) {
            Map<String, Object> map = new HashMap<String, Object>();
            map.put("t", t);

            TypeInfo typeInfo = typeInfoMapper.selectByPrimaryKey(t.getGlType());

            if (typeInfo != null) {
                map.put("typeInfoId", typeInfo.getId());
                map.put("typeInfoTypeName", typeInfo.getTypeName());
            }

            UserInfo userInfo = userInfoMapper.selectByPrimaryKey(t.getUserId());

            if (userInfo != null) {
                map.put("userInfoId", userInfo.getId());
                map.put("userInfoName", userInfo.getName());
            }

            map.put("glLevelStr",
                DataListUtils.getGlLevelNameById(t.getGlLevel() + ""));
            list.add(map);
        }

        Map<String, Object> rs = new HashMap<String, Object>();
        rs.put("data", list);
        rs.put("count", count);
        rs.put("totalPage", totalPage);
        rs.put("pageSize", pageSize);

        return rs;
    }
}

